﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Globalization;
using System.Linq;
using System.Text;
using Dataglot;
using IrishSettings.Commands;

namespace IrishSettings.IntegrationTests
{

    internal class ConfigUtils
    {
        public static DataFactory DefaultDataFactory()
        {
            DataFactory factory = new DataFactory(
                            ConfigurationManager.AppSettings["Dataglot.ConnectionStringName"]
                        );
            return factory;
        }

        public static string ConnectionString()
        {
            string name;
            if (ConfigurationManager.ConnectionStrings["ApplicationServices"] != null)
            {
                name = "ApplicationServices"; //default ASP.ENT Project
            }
            else
            {
                //Another possible convention is to use connection string 0 or 1.
                name = "IrishSettings";
            }

            return ConfigurationManager.ConnectionStrings[name].ConnectionString;
        }

        public static string OleDbConnectionString()
        {
            string name = ConfigurationManager.AppSettings["Dataglot.ConnectionStringName"];
            return ConfigurationManager.ConnectionStrings[name].ConnectionString;//OledbSql
            //return ConfigurationManager.ConnectionStrings["IrishSettings"].ConnectionString;//OledbSql
        }
    }
    public class TestSettings : SettingsApplication
    {
        public TestSettings()
        {
            Construct(new Version(1, 0));
        }

        public TestSettings(Version v)
        {
            Construct(v);
        }

        private void Construct(Version v)
        {
            Application = "TestApp";
            CultureObject = new CultureInfo("en-US");
            VersionObject = v;
            Schema.ValidateAndAdd("culture", Schema.StringItem("culture", "en-US", CultureObject)); //is-is
            Schema.ValidateAndAdd("timezone", Schema.StringItem("timezone", "EST", CultureObject));
            Schema.ValidateAndAdd("fiscalStart", Schema.IntItem("fiscalStart", 10, CultureObject));
            Schema.ValidateAndAdd("workdayStart", Schema.IntItem("workdayStart", 8, CultureObject));
            Schema.ValidateAndAdd("workdayEnd", Schema.IntItem("workdayEnd", 16, CultureObject));
            Schema.ValidateAndAdd("earliestHitDate", Schema.StringItem("earliestHitDate", "1/1/2011", CultureObject));
        }
    }


    public class TestSettingsForUsers : SettingsApplication
    {
        public TestSettingsForUsers()
        {
            Construct(new Version(1, 0));
        }

        public TestSettingsForUsers(Version v)
        {
            Construct(v);
        }

        private void Construct(Version v)
        {
            Application = "TestApp";
            VersionObject = v;
            CultureObject = new CultureInfo("en-US");
            GenericSettingSchema item = Schema.StringItem("culture", "en-US",CultureObject);
            item.Scope = SettingScope.User;
            item.IsUser = true;
            item.IsAnonymous = true;
            Schema.ValidateAndAdd("culture", item); //is-is

            item = Schema.StringItem("timezone", "EST", CultureObject);
            item.Scope = SettingScope.User;
            item.IsUser = true;
            item.IsAnonymous = true;

            Schema.ValidateAndAdd("timezone", item);

            item = Schema.IntItem("fiscalStart", 10, CultureObject);
            item.Scope = SettingScope.User;
            item.IsUser = true;
            item.IsAnonymous = true;

            Schema.ValidateAndAdd("fiscalStart", item);

            item = Schema.IntItem("workdayStart", 8, CultureObject);
            item.Scope = SettingScope.User;
            item.IsUser = true;
            item.IsAnonymous = true;
            Schema.ValidateAndAdd("workdayStart", item);

            item = Schema.IntItem("workdayEnd", 16, CultureObject);
            item.Scope = SettingScope.User;
            item.IsUser = true;
            item.IsAnonymous = true;
            Schema.ValidateAndAdd("workdayEnd", item);

            item = Schema.DateItem("earliestHitDate", new DateTime(2011, 1, 1), CultureObject);
            item.Scope = SettingScope.User;
            item.IsUser = true;
            item.IsAnonymous = true;
            Schema.ValidateAndAdd("earliestHitDate", item);
        }
    }


    public class SchemaUtils
    {
        public static void DeleteApplicationToo(string name, Version version)
        {
            DbApplicationManager manager = new DbApplicationManager(ConfigUtils.DefaultDataFactory() );
            manager.DeleteApplication(name,version);  
        }

        public static TestSettings CreateSampleSchema()
        {
            DataFactory factory = ConfigUtils.DefaultDataFactory();
            using (ClearAllTables cat = new ClearAllTables(factory))
            {
                cat.DeleteAll();
            }
            TestSettings app = new TestSettings();

            DbApplicationManager appManager = new DbApplicationManager(factory);

          DeleteApplicationToo(app.Application, app.VersionObject);
          DbSchemaManager schemaManager = new DbSchemaManager(factory);
          SettingsApplication currentApp = appManager.CreateApplication(factory,app, schemaManager);
          DbSchemaManager manager = new DbSchemaManager(currentApp, factory);

            app.Schema = manager.InsertSchema(app.Schema);
          return app;
        }

        public static TestSettings CreateSampleSchema(Version version)
        {
            DataFactory factory = ConfigUtils.DefaultDataFactory();
            TestSettings app = new TestSettings(version);
            DeleteApplicationToo(app.Application, app.VersionObject);

            DbApplicationManager appManager = new DbApplicationManager(factory);
            DbSchemaManager schemaManager = new DbSchemaManager(factory);
            SettingsApplication currentApp = appManager.CreateApplication(factory, app, schemaManager);
            DbSchemaManager manager = new DbSchemaManager(currentApp,factory);

            app.Schema= manager.InsertSchema(app.Schema);
            return app;
        }
    }
}
